import requests,json
import pandas as pd
key='1436df3e36c45720a1a6371450f01414'

#地理/逆地理编码
#地理编码
def geocode(key,address,city=None,batch='false',sig=None,output='json',callback=None):
    """将详细的结构化地址转换为高德经纬度坐标。且支持对地标性名胜景区、建筑物名称解析为高德经纬度坐标。"""
    url='https://restapi.amap.com/v3/geocode/geo?parameters'
    params={
    'key':key,
    'address':address,
    'city':city,
    'batch':batch,
    'sig':sig,
    'output':output,
    'callback':callback
    }
    response=requests.get(url,params=params)
    results=response.json()['geocodes'][0]['location']
    # return
    return results


#逆地理编码
def regeo(key,location,poitype=None,radius='1000',extensions='base',batch='false',roadlevel=None,sig=None,
          output='json',callback=None,homeorcorp='0'):
    """将经纬度转换为详细结构化的地址，且返回附近周边的POI、AOI信息。"""
    url='https://restapi.amap.com/v3/geocode/regeo?parameters'
    params={
        'key':key,
        'location':location,
        'poitype':poitype,
        'radius':radius,
        'extensions':extensions,
        'batch':batch,
        'sig':sig,
        'output':output,
        'callback':callback,
        'homeorcorp':homeorcorp
    }
    response=requests.get(url,params)
    results=response.json()
    return results


#路径规划
#步行路径规划
def walking(key,origin,location,sig=None,output='json',callback=None):
    """步行路径规划 API 可以规划100KM以内的步行通勤方案，并且返回通勤方案的数据。"""
    url='https://restapi.amap.com/v3/direction/walking?parameters'
    params={
        'key':key,
        'origin':origin,
        'destination':location,
        'sig':sig,
        'output':output,
        'callback':callback,
    }
    response=requests.get(url,params)
    results=response.json()
    # return
    return results


#公交路径规划
def transit(key,origin, location, city, cityid=None, extensions='base', strategy=0, nightflag=0,
            date=None, time=None, sig=None, output='json', callback=None):
    """公交路径规划 API 可以规划综合各类公共（火车、公交、地铁）交通方式的通勤方案，并且返回通勤方案的数据。"""
    url = 'https://restapi.amap.com/v3/direction/transit/integrated?parameters'
    params = {
        'key': key,
        'origin': origin,
        'destination': location,
        'city': city,
        'cityid': cityid,
        'extensions': extensions,
        'strategy': strategy,
        'nightflag': nightflag,
        'date': date,
        'time': time,
        'sig': sig,
        'output': output,
        'callback': callback

    }
    response = requests.get(url, params)
    results = response.json()
    # return
    return results


#驾车路径规划 
def driving(key,origin,destination,extensions):
    """驾车路径规划 API 可以规划以小客车、轿车通勤出行的方案，并且返回通勤方案的数据。"""
    url='https://restapi.amap.com/v3/direction/driving?parameters'
    params={
        'key':key,
        'origin':origin,
        'destination':destination,
        'extensions':extensions
    }
    response=requests.get(url,params)
    results=response.json()
    # return
    return results


#骑车路径规划
def bicycling(key,origin,destination):
    """骑行路径规划用于规划骑行通勤方案，规划时不会考虑路况；考虑天桥、单行线、封路等情况。"""
    url='https://restapi.amap.com/v4/direction/bicycling?parameters'
    params={
        'key':key,
        'origin':origin,
        'destination':destination
    }
    response=requests.get(url,params)
    results=response.json()
    # return
    return results


#货车路径规划
def truck(key,origin,location,size):
    """货车限行分为两种：客观限制（道路客观条件产生的宽高重限制）和政策限行（行政部门规定的限制），路径规划时会根据传入参数分别处理。
       如果传入了宽高重等参数就按参数考虑客观限制，如果没传入则不考虑;
       如果传入了车牌号(province和number)，会考虑政策限行，如果没有传入则不考虑."""
    url='https://restapi.amap.com/v4/direction/truck?parameters'
    params={
        'key':key,
        'origin':origin,
        'destination':location,
        'size':size
    }
    response=requests.get(url,params)
    results=response.json()
    # return
    return results


# 测量距离
def distance(origins, location, type=''):
    """测量距离"""
    url = 'https://restapi.amap.com/v3/distance?parameters'
    params = {
        'key': key,
        'origins': origins,
        'destination': location,
        'type': type

    }
    response = requests.get(url, params)
    results = response.json()
    # return
    return results



#行政区域查询
def district(key,keywords=None,subdistrict='1',page='1',offset='20',extensions='base',filter='',callback=None,output='json'):
    """可以根据用户输入的搜索条件可以帮助用户快速的查找特定的行政区域信息"""
    url='https://restapi.amap.com/v3/config/district?parameters'
    params={
        'key':key,
        'keywords':keywords,
        'subdistrict':subdistrict,
        'extensions':extensions,
        'page':page,
        'offset':offset,
        'filter':filter,
        'callback':callback,
        'output':output
    }
    response=requests.get(url,params)
    results=response.json()
    # return
    return results



#搜索POI
# 关键词搜索
def text(keywords, city='', extensions='', citylimit=''):
    """通过用POI的关键字进行条件搜索，例如：肯德基、朝阳公园等；同时支持设置POI类型搜索，例如：银行"""
    url = 'https://restapi.amap.com/v3/place/text?parameters'
    params = {
        'key': key,
        'keywords': keywords,
        'city': city,
        'extensions': extensions,
        'citylimit': citylimit

    }
    response = requests.get(url, params)
    results = response.json()
    # return
    return results


# 周边搜索
def around(location, keywords=''):
    """在用户传入经纬度坐标点附近，在设定的范围内，按照关键字或POI类型搜索；"""
    url = 'https://restapi.amap.com/v3/place/around?parameters '
    params = {
        'key': key,
        'keywords': keywords,
        'location': location

    }
    response = requests.get(url, params)
    results = response.json()
    # return
    return results


# 多边形搜索
def poly(key,polygon,keywords=None,types=None,offset='20',page='1',extensions='base',sig=None,output='json',callback=None):
    """在多边形区域内进行搜索"""
    url='https://restapi.amap.com/v3/place/polygon?parameters '
    params={
        'key':key,
        'polygon':polygon,
        'keywords':keywords,
        'types':types,
        'offset':offset,
        'page':page,
        'extensions':extensions,
        'sig':sig,
        'output':output,
        'callback':callback
    }
    response=requests.get(url,params)
    results=response.json()
    # return
    return results


# id查询搜索
def detail(key,my_id,sig=None,output="json",callback=None):
    """通过POI ID，查询某个POI详情，建议可同输入提示API配合使用"""
    url = 'https://restapi.amap.com/v3/place/polygon?parameters '
    params = {
       "key": key,
        "id": my_id,
        "sig": sig,
        "output": output,
        "callback": callback

    }
    response = requests.get(url, params)
    results = response.json()
    # return
    return results


#ip定位
def ip(key,ip,sig=None,output="json"):
    """根据用户输入的IP地址，能够快速的帮用户定位IP的所在位置。"""
    url = 'https://restapi.amap.com/v3/ip?parameters'
    params = {
        "key": key,
        "ip": ip,
        "sig": sig,
        "output": output

    }
    response = requests.get(url, params)
    results = response.json()
    # return
    return results


#获取静态地图信息
def amap(key,location,zoom,size='400*400',scale='1',markers=None,labels=None,paths=None,traffic='0',sig=None):
    """静态地图服务通过返回一张地图图片响应HTTP请求，使用户能够将高德地图以图片形式嵌入自己的网页中。用户可以指定请求的地图位置、图片大小、以及在地图上添加覆盖物，如标签、标注、折线、多边形。"""
    url='https://restapi.amap.com/v3/staticmap?parameters'
    params={
    'key':key,
    'location':location,
    'zoom':zoom,
    'scale':scale,
    'markers':markers,
    'labels':labels,
    'paths':paths,
    'traffic':traffic,
    'sig':sig
    }
    r=requests.get(url,params)
    data=r.url
    return data


# 坐标转换
def convert(key,locations,coordays='',sig='',output=''):
    """坐标转换是一类简单的HTTP接口，能够将用户输入的非高德坐标（GPS坐标、mapbar坐标、baidu坐标）转换成高德坐标。"""
    url='https://restapi.amap.com/v3/assistant/coordinate/convert?parameters'
    params={
        'key':key,
        "locations":locations,
        'coordays':coordays,
        'sig':sig,
        'output':output
    }
    response=requests.get(url,params)
    r=response.json()
    return r


# 天气查询
def weather(key,city,extensions='base',output=''):
    """天气查询是一个简单的HTTP接口，根据用户输入的adcode，查询目标区域当前/未来的天气情况。"""
    url='https://restapi.amap.com/v3/weather/weatherInfo?parameters'
    params={
        'key':key,
       'city':city,
        'extensions':extensions,
        'output':output
    }
    response=requests.get(url,params)
    r=response.json()
    return r


# 输入提示
def input(key,keywords,type=None,location=None,city=None,
          citylimit='false',datatype=None,sig=None,output='json',callback=None):
    """输入提示是一类简单的HTTP接口，提供根据用户输入的关键词查询返回建议列表。"""
    url='https://restapi.amap.com/v3/assistant/inputtips?parameters'
    params={
        'key':key,
        'keywords':keywords,
        'type':type,
        'location':location,
        'city':city,
        'citylimit':citylimit,
        'datatype':datatype,
        'sig':sig,
        'output':output,
        'callback':callback
    }
    response=requests.get(url,params)
    r=response.json()
    return r

